package com.uoko.common.math;

import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import org.apache.commons.lang3.Validate;


/**
 * 支持SHA-1/MD5消息摘要的工具类. 返回ByteSource，可进一步被编码为Hex, Base64或UrlSafeBase64
 * 
 * @author calvin
 */
public class Digests {

	private static final String MD5 = "MD5";

	private static final String SHA1 = "SHA-1";

	private static SecureRandom random = new SecureRandom();

	/**
	 * 对输入字符串进行MD5散列.
	 */
	public static byte[] md5(String input) throws NoSuchAlgorithmException{
			return digest(input.getBytes(), MD5, null, 1);
	}
	
	/**
	 * 对输入字符串进行MD5散列,把字节数组转化成字符串返回
	 */
	public static String md5AndHex(String input) throws NoSuchAlgorithmException{
		return bytesConvertToHexString(digest(input.getBytes(), MD5, null, 1));
	}

	/**
	 * 对输入字符串进行sha1散列.
	 */
	public static byte[] sha1(byte[] input) throws NoSuchAlgorithmException{

		return digest(input, SHA1, null, 1);
	}

	public static byte[] sha1(byte[] input, byte[] salt) throws NoSuchAlgorithmException{

		return digest(input, SHA1, salt, 1);
	}

	public static byte[] sha1(byte[] input, byte[] salt, int iterations) throws NoSuchAlgorithmException{

		return digest(input, SHA1, salt, iterations);
	}

	/**
	 * 对字符串进行散列, 支持md5与sha1算法.
	 */
	private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) throws NoSuchAlgorithmException {
			MessageDigest digest = MessageDigest.getInstance(algorithm);

			if (salt != null) {
				digest.update(salt);
			}

			byte[] result = digest.digest(input);

			for (int i = 1; i < iterations; i++) {
				digest.reset();
				result = digest.digest(result);
			}
			return result;

	}

	/**
	 * 生成随机的Byte[]作为salt.
	 * 
	 * @param numBytes
	 *            byte数组的大小
	 */
	public static byte[] generateSalt(int numBytes) {

		Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);

		byte[] bytes = new byte[numBytes];
		random.nextBytes(bytes);
		return bytes;
	}

	/**
	 * 对文件进行md5散列.
	 */
	public static byte[] md5(InputStream input) throws NoSuchAlgorithmException,IOException {

		return digest(input, MD5);
	}

	/**
	 * 对文件进行sha1散列.
	 */
	public static byte[] sha1(InputStream input) throws NoSuchAlgorithmException,IOException {

		return digest(input, SHA1);
	}

	private static byte[] digest(InputStream input, String algorithm) throws NoSuchAlgorithmException,IOException {
			MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
			int bufferLength = 8 * 1024;
			byte[] buffer = new byte[bufferLength];
			int read = input.read(buffer, 0, bufferLength);

			while (read > -1) {
				messageDigest.update(buffer, 0, read);
				read = input.read(buffer, 0, bufferLength);
			}
			return messageDigest.digest();

	}

	/**
	 * 把字节数组转化成字符串返回
	 * 
	 * @param bytes
	 * @return
	 */
	private static String bytesConvertToHexString(byte[] bytes) {

		StringBuilder sb = new StringBuilder();
		for (byte aByte : bytes) {
			String s = Integer.toHexString(0xff & aByte);
			if (s.length() == 1) {
				sb.append("0" + s);
			} else {
				sb.append(s);
			}
		}
		return sb.toString();
	}

}
